#!/usr/bin/env python
#coding=utf-8
import sys
import zipfile
import time
from oss.oss_api import *
if len(sys.argv) == 1:
 endpoint="oss-cn-qingdao.aliyuncs.com"  
 bucket="mybudaipettest"
 object="bin/www.zip"
 sourceDir = "www"
 archive = "www.zip"
 accessKeyId, accessKeySecret="dUnbL1hu1CVqyPRw","ulQbCZl7DGuHqiGrjciMjHXHCMtlw8"
elif sys.argv[1] == "help":
    # print "参数列表:endpoint,accessKeyId,accessKeySecret,bucket,filename"
	# print "例如:./upload.py oss-cn-qingdao.aliyuncs.com dUnbL1hu1CVqyPRw ulQbCZl7DGuHqiGrjciMjHXHCMtlw8 mybudaipettest www www.zip bin/www.zip"
    exit(0)
else:
     endpoint=sys.argv[1]
     bucket=sys.argv[4]
     archive = sys.argv[6]
     object=sys.argv[7]
     sourceDir = sys.argv[5]
     accessKeyId, accessKeySecret=sys.argv[2],sys.argv[3]
f = zipfile.ZipFile(archive,'w',zipfile.ZIP_DEFLATED)
for dirpath, dirnames, filenames in os.walk(sourceDir):
    for filename in filenames:
        sourceFile = os.path.join(dirpath, filename)
        distFile = sourceFile[len('www/'):]
        f.write(sourceFile,distFile)
f.close()
oss = OssAPI(endpoint, accessKeyId, accessKeySecret)
#res = oss.get_bucket("mybudaipettest")
#print "%s\n%s" % (res.status, res.read())
res=oss. copy_object(bucket,object,bucket,object+str(time.time()));
res = oss.put_object_from_file(bucket, object, archive)
#print "%s\n%s" % (res.status, res.getheaders())
print "上传成功:"
print "http://%s.%s/%s" % (bucket,endpoint,object)

